home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 226_01.zip / WORKUP4.C < prev   
Text File  |  1993-04-21  |  16KB  |  468 lines

  1.    /*  HEADER:CUG 226;
  2.        TITLE:"ART-CEE";
  3.        VERSION:1.4;
  4.        DESCRIPTION:"Small artificial intelligence inference engine.
  5.  
  6.             ART-CEE is a rule-based general purpose driver capable of working
  7.             with heirarchical and/or propositional information.";
  8.        KEYWORDS:artificial intelligence, inference engines, database;
  9.        SYSTEM:MS-DOS;
  10.        FILENAME:WORKUP4.C;
  11.        SEE-ALSO:WORKUP0.C, WORKUP2.C, WORKUP3.C, MANUAL.TXT, WORKUP0.H,
  12.             WORKUP1.H, HELP1.AIH, HELP2.AIH, HELP3.AIH, HELP4.AIH, ART-CEE.COM;
  13.        AUTHOR:Dan Cheatham;
  14.        COMPILER:MIX-C;
  15.    */
  16.  
  17.  
  18.          /*  art-cee  artificial intelligence inference engine    */
  19.          /*                                                       */
  20.          /*  file:  workup1.C    --  external data definitions    */
  21.          /*                          main function                */
  22.          /*                          initial input and parse      */
  23.          /*                          help                         */
  24.          /*                                                       */
  25. #include "stdio"
  26. #include "workup0.h"
  27.     float    dfwd, drev, ff1, ft[MAX][MAX];
  28.     char     indff[80], subjs[MAX][77], currfile[32], menu_sw,
  29.              oride_sw, filebuf[81];
  30.     int      x1, x2, filestat, srchdpth, assumpno;
  31.  
  32.  
  33. main()
  34.     {  wakeup(); menu_sw = 'Y'; oride_sw = 'Y'; assumpno = 0;
  35.        x1 = 50; filestat = 0; dfwd = 100.0 ; drev = 0.0; srchdpth = 3;
  36.        strcpy(currfile,"ART-CEE.DAT");
  37.          while (toupper(indff[0]) != 'X')
  38.              { mainin();
  39.                if (x1 > 2 && x2 != 0)
  40.                   exec_string();
  41.                else
  42.                   { if (x1 == 1)
  43.                    commands();
  44.                     else
  45.                        { printf("\nInvalid entry.");
  46.                          marktime(); } } }
  47.  
  48.        printf("\nSave database (Y/N)? ");
  49.               gets(indff);
  50.               if (toupper(indff[0]) == 'Y')
  51.                   savedata(0,MAX);
  52. }
  53.   
  54. marktime()
  55.    { char xx; printf("\nPress return to proceed."); xx = getchar(); }
  56.  
  57.  
  58. wakeup()
  59.    { system("cls");
  60.       printf("\n      AAAAAA   RRRRRRRR  TTTTTTTT      CCCCCCCC  EEEEEEEE  EEEEEEEE");
  61.       printf("\n     AA    AA  RR    RR     TT        CC         EE        EE");
  62.       printf("\n     AA    AA  RR    RR     TT        CC         EE        EE");
  63.       printf("\n     AAAAAAAA  RRRRRRRR     TT  ++++  CC         EEEEEE    EEEEEE");
  64.       printf("\n     AA    AA  RR  RR       TT  ++++  CC         EE        EE");
  65.       printf("\n     AA    AA  RR   RR      TT        CC         EE        EE");
  66.       printf("\n     AA    AA  RR    RR     TT         CCCCCCCC  EEEEEEEE  EEEEEEEE\n\n");
  67.       printf("\n                            By Dan Cheatham\n\n\n\n\n\n\n");
  68.       initialize();
  69. }
  70.   
  71. showmenu()
  72.       {  system("cls");
  73.          printf("COMMANDS:                          SETTABLE DEFAULTS:\n");
  74.          printf("   B batch group     L load          Forward        %f\n",dfwd);
  75.          printf("   C change subject  P print         Reverse        %f\n",drev);
  76.          printf("   D drop rule       S save          Assumptions    %d\n",assumpno);
  77.          printf("   G manual group    T think         thinK depth    %d\n",srchdpth);
  78.          printf("   H help            V view          allow Override %c\n",oride_sw);
  79.          printf("   I initialize      X exit          show Menu      %c\n",menu_sw);
  80.          printf("RULES:\n");
  81.          printf("   IF <subject> THEN <predicate>\n");
  82.          printf("QUERIES:\n");
  83.          printf("   WHO <subject> <?>\n");
  84.          printf("   WHAT <subject> <?>\n");
  85.          printf("   DESCRIBE <subject>\n");
  86.          printf("   IF <subject> THEN <predicate> ?\n\n");
  87. }
  88.  
  89. commands()
  90.      { switch(toupper(indff[0]))
  91.         { case 'A':  setassump()                 ; break;
  92.           case 'B':  filegroup()                 ; break;
  93.           case 'C':  chgsubj()                   ; break;
  94.           case 'D':  droprule()                  ; break;
  95.           case 'F':  setfwd()                    ; break;
  96.           case 'G':  manugroup()                 ; break;
  97.           case 'H':  help()                      ; break;
  98.           case 'K':  setdepth()                  ; break;
  99.           case 'I':  initialize()                ; break;
  100.           case 'L':  loaddata()                  ; break;
  101.           case 'M':  menu_sw  = toggle(menu_sw)  ; break;
  102.           case 'O':  oride_sw = toggle(oride_sw) ; break;
  103.           case 'P':  hardcopy()                  ; break;
  104.           case 'R':  setrev()                    ; break;
  105.           case 'S':  presave()                   ; break;
  106.           case 'T':  think()                     ; break;
  107.           case 'V':  view()                      ;
  108.           }
  109. }
  110.  
  111. setassump()
  112.    { int qa1, qa2; char dumment[2];
  113.      qa1 = MAX - 3;
  114.      printf("\nWhat is the maximum number of assumptions to allow?");
  115.      printf("\nAbsolute maximum is %d, absolute minimum is zero.",qa1);
  116.      printf("\nYour choice: ");
  117.      scanf("%d",&qa2); gets(dumment);
  118.  
  119.      if (qa2 < 0)
  120.         assumpno = 0;
  121.      else
  122.         { if (qa2 > qa1)
  123.             assumpno = qa1;
  124.           else
  125.             assumpno = qa2; }
  126. }
  127.  
  128. setdepth()
  129.    { int sd1, sd2; char dumment[2];
  130.      sd1 = MAX - 1;
  131.      printf("\nHow many levels shall be examined in thinking?");
  132.      printf("\nAbsolute maximum is %d, absolute minimum is 3.",sd1);
  133.      printf("\nYour choice: ");
  134.      scanf("%d",&sd2); gets(dumment);
  135.      if (sd2 < 3)
  136.         srchdpth = 3;
  137.      else
  138.         { if (sd2 > sd1)
  139.             srchdpth = sd1;
  140.           else
  141.             srchdpth = sd2;}
  142. }
  143.  
  144. initialize()
  145.        { int sx1; char empstr[] = "@@@@@"; 
  146.          printf("\n\nInitializing storage.");
  147.          for (sx1 = 0; sx1 < MAX; sx1++)
  148.           { blank1pt(sx1);
  149.             strcpy(subjs[sx1],empstr); }
  150. }
  151.  
  152. setfwd()
  153.   { ff1 = dfwd;
  154.     dflt("forward");
  155.     dfwd = ff1;
  156. }
  157.  
  158. setrev()
  159.   { ff1 = drev;
  160.     dflt("reverse");
  161.     drev = ff1;
  162. }
  163.  
  164. toggle(cv)
  165.    char *cv;
  166.  { if (cv == 'Y')
  167.       return('N');
  168.    else
  169.       return('Y');
  170. }
  171.  
  172. dflt(st1)
  173.       char *st1[ ];
  174.    {  float ff2; char tx;
  175.       printf("\n\nEnter new default %s percentage",st1);
  176.       printf("\n   100 = certainty, 0 = never, must be at least zero.  ");
  177.       scanf("%f",&ff2); tx = getchar();
  178.       if (ff2 < 0 || ff2 > 100)
  179.        { printf("\nInvalid entry."); marktime(); }
  180.       else
  181.           ff1 = ff2;
  182. }
  183.  
  184. disphelp(tt)
  185.      char *tt[ ];
  186.    { int      wk2; char tc; FILE *infile;
  187.      infile = NULL;
  188.      tc = "C";
  189.      while (infile == NULL && tc != 'A' && tc != 'a')
  190.        { if (filestat == 0)
  191.           { printf("\n\n Insert disk containing ART-CEE help files.");
  192.             printf("\n Press 'A' to abort, any other key to proceed.");
  193.             tc = getchar(); }
  194.           if (toupper(tc)  != 'A')
  195.             { infile = fopen(tt,"r");
  196.               filestat  = infile; } }
  197.      if (toupper(tc) != 'A' && infile != NULL);
  198.        { wk2 = 0; system("cls");
  199.          while (fgets(filebuf ,81,infile) != NULL)
  200.            { if (wk2 > 20)
  201.                { marktime(); system("cls"); wk2 = 0; }
  202.              printf("%s",filebuf );
  203.              wk2++; }
  204.              fclose(infile); }
  205. }
  206.  
  207. help()
  208.    { char tcc[2];
  209.      disphelp("help1.aih");
  210.      gets(tcc);
  211.      while(tcc [0] == 'R' || tcc [0] == 'C' || tcc [0] == 'r' ||
  212.            tcc [0] == 'c' || tcc [0] == 'Q' || tcc [0] == 'q')
  213.        { if (tcc [0] == 'R' || tcc [0] == 'r')
  214.               disphelp("help2.aih");
  215.          else
  216.            { if (tcc[0] == 'C' || tcc [0] == 'c')
  217.                 disphelp("help4.aih");
  218.              else
  219.                 disphelp("help3.aih"); }
  220.          printf("\n\n Press 'R' for rules, 'Q' for queries, 'C' for commands,");
  221.          printf("\n anything else to return to main input. ");
  222.          gets(tcc);}
  223. }
  224.  
  225. failfile()
  226.    { printf("\n\nFile procedure has failed."); marktime();
  227. }
  228.  
  229. exec_string()
  230.     {  char pred[77]; int rs1, rs2, rs3, rs4, findsub();
  231.        rs4 = 1;
  232.        if ( x2 != 1 && x2 != -1 && x2 != 9)
  233.           { rs2 = findsub(indff);
  234.             if (rs2 > MAX)
  235.                { printf("\nSubject not found."); marktime(); }
  236.             else
  237.                  halfques(rs2,rs4); }
  238.        else
  239.           { rs1 = parsepred(indff," THEN ",pred,strlen(indff),6);
  240.             if (rs1 != 1)
  241.               { printf("\nInvalid predicate."); marktime(); }
  242.             else
  243.              { rs2 = findsub(indff);
  244.                rs3 = findsub(pred);
  245.                    if (x2 == -1 && rs2 < MAX && rs3 < MAX)
  246.                      fullques(rs2,rs3);
  247.                    else
  248.                      { if (x2 == 9 && rs2 < MAX && rs3 < MAX)
  249.                          dropem(rs2,rs3);
  250.                        else
  251.                          { if (x2 == 1)
  252.                               newrule(pred,rs2,rs3);
  253.                            else
  254.                               { printf("\nSubject or predicate not found.");
  255.                                 marktime(); }}}}}
  256. }
  257.  
  258. findsub(dd)
  259.    char *dd[ ];
  260.    { int fs1, fs2, fs3;
  261.      fs1 = MAX + 1; fs2 = 0;
  262.      while (fs2 < MAX && fs1 > MAX)
  263.         { fs3 = strcmp(dd,subjs[fs2]);
  264.           if (fs3 == 0)
  265.              fs1 = fs2;
  266.           fs2++; }
  267.     return(fs1);
  268. }
  269.  
  270. newrule(dd,r1,r2)
  271.     char *dd; int r1,r2;
  272.   { char pred[77]; int r3, r4, r5, r6, r7, r8;
  273.  
  274.     if (r1 == r2 && r1 < MAX)
  275.        r5 = 1;
  276.     else
  277.       { r3 = MAX; r4 = MAX;
  278.         for (r6 = 0; r6 < MAX; r6++)
  279.           { if (r1 > MAX && subjs[r6][0] == '@')
  280.                r1 = r6;
  281.             if (r2 > MAX && subjs[r6][0] == '@' && r1 != r6)
  282.                r2 = r6;}
  283.         if (r1 < MAX && r2 < MAX)
  284.           { r5 = 2;
  285.             strcpy(subjs[r1],indff);
  286.             strcpy(subjs[r2],dd); }
  287.         else
  288.             r5 = 3; }
  289.      if (r5 == 1)
  290.         { printf("\nTautology entered.  Cannot continue."); marktime(); }
  291.      else
  292.         { if (r5 == 3)
  293.            { printf("\nDatabase full.  Cannot add rule."); marktime(); }
  294.           else
  295.              percts(r1,r2);  }
  296. }
  297.  
  298. percts(d1,d2)
  299.    int d1, d2;
  300.    { char newpctg[3], tmpchr; float ff2;
  301.      if (ft[d1][d2] != 0)
  302.       { printf("\n\nRule exists--probability %5.2f pct.  Change percentage? ",
  303.            ft[d1][d2]);
  304.         gets(newpctg);
  305.         if (toupper(newpctg[0]) == 'Y')
  306.           { printf("\nEnter new percent of time true: ");
  307.             scanf("%f",&ff2); tmpchr = getchar();
  308.               if (ff2 < 0 || ff2 > 100)
  309.                 { printf("\nInvalid value.  Old value kept."); marktime(); }
  310.               else
  311.                 ft[d1][d2] = ff2; }
  312.         else
  313.            {} }
  314.      else
  315.         { if (oride_sw != 'Y')
  316.             { ft[d1][d2] = dfwd; ft[d2][d1] = drev; }
  317.           else
  318.             { printf("\nEnter percent of time that the following are true.");
  319.               printf("\nIf %s \n   then %s: ",subjs[d1],subjs[d2]);
  320.               scanf("%f",&ff2); tmpchr = getchar();
  321.               {if (ff2 < 0 || ff2 > 100)
  322.                 { printf("\nInvalid value.  Default used.");
  323.                   ft[d1][d2] = dfwd; }
  324.                else
  325.                   ft[d1][d2] = ff2;  }
  326.               printf("\nIf %s \n   then %s: ",subjs[d2],subjs[d1]);
  327.               scanf("%f",&ff2); tmpchr = getchar();
  328.               if (ff2 < 0 || ff2 > 100)
  329.                 { printf("\nInvalid value.  Default used.");
  330.                   ft[d2][d1] = drev;
  331.                   marktime(); }
  332.               else
  333.                   ft[d2][d1] = ff2; } }
  334. }
  335.  
  336. halfques(d1,d2)
  337.    int d1, d2;
  338.    { int wh1, wh2;
  339.      if (d2 == 1)
  340.         system("cls");
  341.      wh2 = 1; printf("\nForward references for %s",subjs[d1]);
  342.      for (wh1 = 0; wh1 < MAX; wh1++)
  343.         { if (wh2 > 18 && d2 == 1)
  344.             { marktime(); system("cls"); wh2 = 1;
  345.               printf("\nForward references for %s",subjs[d1]); }
  346.            if (ft[d1][wh1] > 0)
  347.              { printf("\n   then %s  %5.2f pct.",subjs[wh1],ft[d1][wh1]); wh2++;
  348.                }}
  349.      printf("\n\nReverse references for %s",subjs[d1]); wh2++; wh2++;
  350.      for (wh1 = 0; wh1 < MAX; wh1++)
  351.         { if (wh2 > 18 && d2 == 1)
  352.             { marktime(); system("cls"); wh2 = 1;
  353.               printf("\nReverse references for %s",subjs[d1]); }
  354.            if (ft[wh1][d1] > 0)
  355.              { printf("\n   if   %s  %5.2f pct.",subjs[wh1],ft[wh1][d1]); wh2++;
  356.               }}
  357.      if (d2 == 1)
  358.          marktime();
  359. }
  360.  
  361. view()
  362.    { int lines,  wh2, wh3, wh4, wh5;
  363.      system("cls"); lines = 0;
  364.      for (wh3 = 0; wh3 < MAX; wh3++)
  365.         { if (subjs[wh3][0] != '@')
  366.             { if (lines > 20)
  367.                 { marktime(); system("cls"); lines = 0; }
  368.              printf("\nSubject #%d, %s",wh3,subjs[wh3]);
  369.              wh4 = 0; wh5 = 0;
  370.              for (wh2 = 0; wh2 < MAX; wh2++)
  371.                { if (ft[wh3][wh2] > 0)
  372.                    wh4++;
  373.                  if (ft[wh2][wh3] > 0)
  374.                    wh5++; }
  375.              printf("\n     Forward references:  %d",wh4);
  376.              printf("\n     Reverse references:  %d",wh5);
  377.              lines++; lines++; lines++;}}
  378.      marktime();
  379. }
  380.  
  381. chgsubj()
  382.    { char newsub[77];  int rs1, rs2, rs3, rs4, findsub();
  383.      printf("\n\nEnter subject you wish to change:  ");
  384.      gets(newsub);
  385.      rs2 = findsub(newsub);
  386.      if (rs2 > MAX)
  387.         { printf("\n\nSubject not found."); marktime(); }
  388.      else
  389.         { printf("\n\nEnter new subject.  It will NOT be edited.");
  390.           printf("\nThe subject: ");
  391.           gets(newsub);
  392.           strcpy(subjs[rs2],newsub);}
  393. }
  394.  
  395. hardcopy()
  396.    { FILE *outfile;
  397.      int wk1, wk2, wk3;
  398.      outfile = fopen("prn:","w");
  399.      if (outfile == NULL)
  400.          printf("\nPrinter not found.  Cannot proceed.");
  401.      else
  402.        { for (wk1 = 0; wk1 < MAX; wk1++)
  403.          { if (subjs[wk1][0] != '@')
  404.              { wk3 = fprintf(outfile,"IF %s\n",subjs[wk1]);
  405.                for (wk2 = 0; wk2 < MAX; wk2++)
  406.                  { if (ft[wk1][wk2]  > 0)
  407.                      wk3 = fprintf(outfile,"   then %s, %8.5f\n",subjs[wk2],ft[wk1][wk2]);
  408.                   }
  409.             wk3 = fprintf(outfile,"Then %s\n",subjs[wk1]);
  410.                for (wk2 = 0; wk2 < MAX; wk2++)
  411.                  { if (ft[wk2][wk1] > 0)
  412.                      wk3 = fprintf(outfile,"   if %s, %8.5f\n",subjs[wk2],ft[wk2][wk1]);
  413.          }}}
  414.          wk3 = fflush(outfile);
  415.          wk3 = fclose(outfile); }
  416. }
  417.  
  418. droprule()
  419.    { char dr4[81];
  420.      printf("\nEnter rule to be dropped.\n");
  421.      gets(indff);
  422.      x1 = strlen(indff); x2 = 0;
  423.      if (x1 > 7)
  424.         x2 = parsesub(indff,"IF THE ",dr4,x1,7);
  425.      if (x2 == 0 && x1 > 6)
  426.         x2 = parsesub(indff,"IF AN ",dr4,x1,6);
  427.      if (x2 == 0 && x1 > 5)
  428.         x2 = parsesub(indff,"IF A ",dr4,x1,5);
  429.      if (x2 == 0 && x1 > 3)
  430.         x2 = parsesub(indff,"IF ",dr4,x1,3);
  431.      if (x2 != 0)
  432.         { x2 = 9;
  433.           strcpy(indff,dr4);
  434.           exec_string(); }
  435.      else
  436.         { printf("\nInvalid syntax.  Cannot proceed."); marktime(); }
  437. }
  438.  
  439. dropem(ww1,ww2)
  440.         int ww1, ww2;
  441.       { int ws2, ws3, ws4; char empstr[] = "@";
  442.         if (ft[ww1][ww2] < 0 || ft[ww1][ww2] == 0)
  443.             printf("Rule not found.");
  444.         else
  445.           { ft[ww1][ww2] = 0; printf("\n\n  Rule dropped.");
  446.             ws2 = 0; ws3 = 0;
  447.             for (ws4 = 0; ws4 < MAX; ws4++)
  448.              { if (ft[ww1][ws4] > 0 || ft[ws4][ww1] > 0)
  449.                   ws2 = 1;
  450.                if (ft[ww2][ws4] > 0 || ft[ws4][ww2] > 0)
  451.                   ws3 = 1; }
  452.             if (ws2 == 0)
  453.              { strcpy(subjs[ww1],empstr); blank1pt(ww1);
  454.                printf("\n  Subject removed."); }
  455.             if (ws3 == 0)
  456.              { strcpy(subjs[ww2],empstr); blank1pt(ww2);
  457.                printf("\n  Predicate removed."); }}
  458.             marktime();
  459. }
  460.  
  461. blank1pt(wx)
  462.    int wx;
  463.        { int sz2;
  464.          for (sz2 = 0; sz2 < MAX; sz2++ )
  465.            ft[wx][sz2] = 0;
  466.          ft[wx][wx] = -100;
  467. }
  468.